//
//  ChangePwdVC.m
//  KeleAppProject
//
//  Created by Cen Zhou on 2018/1/18.
//  Copyright © 2018年 Cen Zhou. All rights reserved.
//

#import "ChangePwdVC.h"

@interface ChangePwdVC ()

@property (nonatomic, copy) NSString *pwdStr;
@property (nonatomic, strong) UIButton *showBtn;
@property (nonatomic, strong) UIButton *deleteBtn;
@property (nonatomic, strong) UITextField *inputTF;
@end

@implementation ChangePwdVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"修改密码";
    self.view.backgroundColor = [UIColor whiteColor];
    [self loadSubviewsOnChangePwdVC];
}

- (void)loadSubviewsOnChangePwdVC{
    UIView *topLine = [[UIView alloc]init];
    topLine.backgroundColor = KLColor_B3;
    [self.view addSubview:topLine];
    [topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.mas_equalTo(1);
    }];
    
    UIView *topView = [[UIView alloc]init];
    topView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topLine.mas_bottom);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(60*HEIGHT);
    }];
    
    UITextField *inputTF = [[UITextField alloc]init];
    inputTF.placeholder = @"请输入新密码（6~20位英文或数字）";
    inputTF.textColor = KLColor_B6;
    inputTF.secureTextEntry = YES;
    self.inputTF = inputTF;
    inputTF.clearButtonMode=UITextFieldViewModeWhileEditing;
    inputTF.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14*WIDTH];
    [[inputTF rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(UITextField *inputTF) {
        self.pwdStr = inputTF.text;
        self.showBtn.hidden = self.deleteBtn.hidden = !(inputTF.text.length > 0);
    }];
    [topView addSubview:inputTF];
    [inputTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topView).offset(28*HEIGHT);
        make.right.equalTo(topView).offset(-24*WIDTH);
        make.left.equalTo(topView).offset(26*WIDTH);
    }];
    
    
    UIButton *showPwdBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [showPwdBtn setBackgroundImage:[UIImage imageNamed:@"icon_kaijianmima_pre"] forState:UIControlStateNormal];
    [topView addSubview:showPwdBtn];
    showPwdBtn.hidden = YES;
    self.showBtn = showPwdBtn;
    [showPwdBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topView).offset(30*HEIGHT);;
        make.right.equalTo(topView).offset(-57*WIDTH);
    }];
    [[showPwdBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(UIButton *showBtn) {
        self.inputTF.secureTextEntry = !self.inputTF.secureTextEntry;
        if (self.inputTF.secureTextEntry) {
            [showPwdBtn setBackgroundImage:[UIImage imageNamed:@"icon_kaijianmima_pre"] forState:UIControlStateNormal];
        }else{
            [showPwdBtn setBackgroundImage:[UIImage imageNamed:@"icon_kaijianmima"] forState:UIControlStateNormal];
        }
    }];
    
    
    
    UIView *bottomLine = [[UIView alloc]init];
    bottomLine.backgroundColor = KLColor_B3;
    [topView addSubview:bottomLine];
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.centerX.equalTo(topView);
        make.width.mas_equalTo(325*WIDTH);
        make.height.mas_equalTo(1);
    }];
    
    
    // 下一步按钮
    
    UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    nextBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16*WIDTH];
    [nextBtn setBackgroundImage:[UIImage createImageWithColor:KLColor_A1] forState:UIControlStateNormal];
    nextBtn.layer.masksToBounds = YES;
    nextBtn.layer.cornerRadius = 22*HEIGHT;
    [self.view addSubview:nextBtn];
    RAC(nextBtn,enabled) = [RACSignal combineLatest:@[RACObserve(self, pwdStr)] reduce:^id(NSString *pwdStr){
        BOOL enable;
        enable = pwdStr.length>0;
        return @(enable);
    }];
    [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(topLine.mas_bottom).offset(105*HEIGHT);
        make.width.mas_equalTo(345*WIDTH);
        make.height.mas_equalTo(44*HEIGHT);
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
